package com.liang.blog.config;

import com.liang.blog.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Enumeration;
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //验证token是否合法 token或者cookie都是放在handle里面
        String userToken = request.getHeader("user_token_header");
        Claims claims = JwtUtils.verify(userToken);
        log.info("从header中获取信息，token:" + userToken);
        if (claims == null) {
            //token校验失败
            response.setStatus(401);
//            response.getOutputStream().write();
//            response.setContentType();
            return false;
        }
       return true;
    }

}
